package com.mryang.hds.service.impl;

import com.mryang.hds.configs.AppException;
import com.mryang.hds.entity.PunchInfo;
import com.mryang.hds.entity.QuestionSolution;
import com.mryang.hds.mapper.PunchInfoMapper;
import com.mryang.hds.mapper.PunchRecordMapper;
import com.mryang.hds.mapper.UserMapper;
import com.mryang.hds.service.IPunchInfoService;
import com.mryang.hds.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;


/**
 * @author Genius
 * @version 1.0.0
 * @ClassName PunchInfoServiceImpl.java
 * @Description TODO
 * @createTime 2023年01月13日 15:19:00
 */

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@Service
public class PunchInfoServiceImpl implements IPunchInfoService {

    @Resource
    private PunchInfoMapper punchInfoMapper;

    @Resource
    private PunchRecordMapper punchRecordMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 获取所有题目信息
     * @return
     */
    @Override
    public List<String> getAllQuestion() {
        try {
            List<String> list = new LinkedList<>();

            for (QuestionSolution questionSolution : punchInfoMapper.getAllQuestion()) {
                list.add(questionSolution.getQuestionId()+"、"+questionSolution.getQuestionName());
            }

            return list;

            // return punchInfoMapper.getAllQuestion();
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.SUBJECT_FAIL);
        }

    }

    /**
     * 获取题目及选项信息
     * @return
     */
    @Override
    public TreeMap<String, List<QuestionSolution>> getQuestionSolution() {

        try {
            TreeMap<String, List<QuestionSolution>> qsMap = new TreeMap<>();

            List<QuestionSolution> allSelectQuestion = punchInfoMapper.getAllQuestion();

            for (QuestionSolution qs : allSelectQuestion) {
                List<QuestionSolution> questionSolution = punchInfoMapper.getQuestionSolution(qs.getQuestionId());
                qsMap.put(qs.getQuestionId()+"、"+qs.getQuestionName(),questionSolution);
            }

            return qsMap;
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.SUBJECT_FAIL);
        }


    }

    /**
     * 获取选择题及选项信息
     * @return
     */
    @Override
    public TreeMap<String, List<QuestionSolution>> getSelectQuestionSolution() {

        try {
            TreeMap<String, List<QuestionSolution>> qsMap = new TreeMap<>();

            List<QuestionSolution> allSelectQuestion = punchInfoMapper.getAllSelectQuestion();

            for (QuestionSolution qs : allSelectQuestion) {
                List<QuestionSolution> questionSolution = punchInfoMapper.getQuestionSolution(qs.getQuestionId());
                qsMap.put(qs.getQuestionId()+"、"+qs.getQuestionName(),questionSolution);
            }

            return qsMap;
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.SUBJECT_FAIL);
        }


    }


    /**
     * 健康打卡操作
     * @param punchInfoList 打卡数据信息
     * @param uid 用户编号
     * @param time 打卡时间
     */
    @Transactional
    @Override
    public void addPunchInfo(List<PunchInfo> punchInfoList,int uid,String time,String temp) {

        try {
            int flag = 0;
            for (PunchInfo punchInfo : punchInfoList) {
                // 添加健康数据信息
                punchInfoMapper.addPunchInfo(punchInfo);
                if (punchInfo.getDataException().equals("异常")){
                    flag++;
                }
            }
            if (flag > 0){
                // 修改用户打卡字段信息
                userMapper.updatePunchInfo(uid,-1);
                // 添加健康打卡信息（某一天是否打卡）
                punchRecordMapper.addPunchRecord(uid,time.split(" ")[0],"异常",temp);
            }else{
                // 修改用户打卡字段信息
                userMapper.updatePunchInfo(uid,1);
                // 添加健康打卡信息（某一天是否打卡）
                punchRecordMapper.addPunchRecord(uid,time.split(" ")[0],"正常",temp);

            }

        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.PUNCH_FAIL);
        }



    }
}
